package mybatisTest.reflect.type;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TypeTest<T extends String> {

    public List<String> l1=new ArrayList<>();
    public Map<String,String> m1=new HashMap<>();

    public List<T> tl=null;

    SubClassA<Long> subClassA=new SubClassA<>();

    @Test
    public void paratest() throws NoSuchFieldException {
        Field l1 = TypeTest.class.getDeclaredField("l1");
        Type genericType = l1.getGenericType();
        System.out.println(genericType.getTypeName());

        ParameterizedType pt = (ParameterizedType) genericType;

        Type[] actualTypeArguments = pt.getActualTypeArguments();

        Type rawType = pt.getRawType();
        Type ownerType = pt.getOwnerType();

        for (Type e:actualTypeArguments){
            System.out.println(e.getClass().getName());
            System.out.println(e.getTypeName());
        }

        System.out.println(rawType.getTypeName());//java.util.List
        System.out.println(ownerType.getTypeName());//
    }

    @Test
    public void typeVariabletest(){
        TypeVariable<Class<TypeTest>>[] typeParameters = TypeTest.class.getTypeParameters();
        for (TypeVariable e:typeParameters){
            System.out.println(e.getName());
            System.out.println( e.getGenericDeclaration());
            System.out.println(e.getTypeName());
        }
    }
    @Test
    public void test() throws NoSuchFieldException {
        Field field = TypeTest.class.getDeclaredField("subClassA");
        Type genericType = field.getGenericType();
    }
    @Test
    public void test2() throws NoSuchFieldException {
        Field map = SubClassA.class.getDeclaredField("map");
        System.out.println(map.getName());
    }
}
